layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/device/upgrade/task/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,size:'lg'
    ,cols: [[
      {field: 'id', title: '任务ID'}
      ,{field: 'deviceSn', title: '设备序列号'}
      ,{field: 'deviceTypeId', title: '设备类型', templet: function (record){
          if(record.deviceType){
              return record.deviceType.name + '('+record.deviceTypeId+')';
          }else {
              return record.deviceTypeId;
          }
          }}
      ,{field: 'versionNumber', title: '升级版本号'}
      ,{field: 'stateName',title: '状态'}
      ,{field: 'upgradeDate',title: '升级时间'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 200, align:'center', fixed: 'right', templet:function(record){
              var html = '';
              html = html + '<a class="layui-btn layui-btn-xs" lay-event="info">查看</a>';
              if(record.state == 1){
                  html = html + '<a class="layui-btn layui-btn-xs" lay-event="toExecute">执行</a>';
              }
              if(record.state != 2){
                  html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
              }
              return html;
          }}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      add:function(){
          layer.open({
              type: 2
              , title: '新增设备固件升级任务'
              , content: layui.setter.webRoot + '/device/upgrade/task/add.html'
              , maxmin: false
              , area: ['600px', '400px']
              , btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/device/upgrade/task/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,info:function(obj){
            layer.open({
                type: 2
                ,title: '查看设备固件升级任务'
                ,content: layui.setter.webRoot + '/device/upgrade/task/info.html?id='+obj.data.id
                ,maxmin: true
                ,area: ['90%', '90%']
              });
      }
      ,toExecute:function(obj){
          layer.confirm('确定要执行该固件升级任务吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/device/upgrade/task/execute.do',
                  data:{id:obj.data.id},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          table.reload('LAY-table');
                          layer.msg('执行成功',{icon: 1});
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,del:function(obj){
    	layer.confirm('确定要删除该固件升级任务吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/device/upgrade/task/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
     }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('device_upgrade_task_list', {})
});